package com.hy.utils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.sql.SQLException;

/**
 * Created With IntelliJ IDEA.
 * Descriptions:事务管理类
 * author: Mr.Du
 * Date: 2022/9/2
 * Time: 11:35
 */
@Component("transactionManager")
public class TransactionManager {
    
    @Autowired
    private ConnectionUtils connectionUtils;
    
    // 开启手动事务控制
    public void beginTransaction() throws SQLException {
        System.out.println("开启事务");
        connectionUtils.getConnection().setAutoCommit(false);
    }
    
    
    // 提交事务
    public void commit() throws SQLException {
        System.out.println("提交事务");
        connectionUtils.getConnection().commit();
    }
    
    
    // 回滚事务
    public void rollback() throws SQLException {
        System.out.println("回滚事务");
        connectionUtils.getConnection().rollback();
    }
}
